capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local crime "";
local output "";


/* 
RD estimates (joint crime and employment)
*/


local exp "prwora_cXe";

local controls_all "n_male i.n_firstage i.n_diag1cat n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_fam_singmom n_fam_nopar i.n_earnhhbins i.state" ;
local controls_no  "" ; 

local bwidth_list "264" ;
local cov_list   "all no" ; 
local demo_list "all remhigh remlow ueallhigh uealllow crimehigh crimelow pophigh poplow demhigh demlow parhigh parlow male female singno twopar othmen nonment intel phys white black nonw" ;

local vars "n_crime2emp* n_crimeno2* n_crime2no*" ; 
local poly_list "1(1)1" ;
local bwidth_close = 90 ;


use "`crime'\prep2\crime_prwora_prepforreg.dta", clear ;

/* Create predicted removal probability */
xi: reg n_age18unfav 
	`controls_all' i.dibdig
	if n_post == 1 ;
predict hat_removal ;

summ hat_removal, det ;
gen byte demo_q25 		= (hat_removal <  r(p25)) if hat_removal != . ;
gen byte demo_q2575 	= (hat_removal >= r(p25) & hat_removal < r(p75)) if hat_removal != . ;
gen byte demo_q75 		= (hat_removal >= r(p75) & hat_removal != .) if hat_removal != . ;
gen byte demo_remlow 	= (hat_removal <  r(p50)) if hat_removal != . ;
gen byte demo_remhigh 	= (hat_removal >= r(p50) & hat_removal != .) if hat_removal != . ;

keep if abs(dobdd_run) <= `bwidth_list' ;

/* Subsamples */
gen demo_all = 1 ;
gen byte demo_othment	= (n_diag1_mental == 1 & (substr(dibdig,1,3) != "317" & substr(dibdig,1,3) != "318" & substr(dibdig,1,3) != "319" )) ;
gen byte demo_nonment 	= (demo_othment == 0) ;
gen byte demo_intel 	= (n_diag1_mental == 1 & (substr(dibdig,1,3) == "317" | substr(dibdig,1,3) == "318" | substr(dibdig,1,3) == "319" )) ;
gen byte demo_phys 		= (n_diag1_mental != 1) ;
gen byte demo_nonphys 	= (n_diag1_mental == 1) ;
gen byte demo_twopar 	= (n_fam_singmom != 1 & n_fam_nopar != 1) ;
gen byte demo_singno 	= (n_fam_singmom == 1) | (n_fam_nopar ==1) ;
gen byte demo_nopar  	= (n_fam_nopar == 1) ;
gen byte demo_male 	 	= (n_male ==1) ;
gen byte demo_female 	= (n_male !=1) ;
gen byte demo_white		= (race == "W") ;
gen byte demo_black 	= (race == "B") ;
gen byte demo_nonw		= (race != "W") ;

egen cnty_ue97 = rowmean(cnty_urate_1997-cnty_urate_1998) ;
egen cnty_uegr = rowmean(cnty_urate_2007-cnty_urate_2012) ;
egen cnty_ueall = rowmean(cnty_urate_1997-cnty_urate_2017) ;
rename cnty_crimerate cnty_crime ;
rename cnty_share_blue cnty_dem ;
rename cnty_pop2000 cnty_pop ;

foreach cnty in crime dem pop ue97 uegr ueall { ;
	summ cnty_`cnty', det ;
	gen byte demo_`cnty'high = (cnty_`cnty' > r(p50) & cnty_`cnty' != .) ;
	gen byte demo_`cnty'low = (cnty_`cnty' <= r(p50) & cnty_`cnty' != .) ;
	replace demo_`cnty'high = . if cnty_`cnty' == . ;
	replace demo_`cnty'low = . if cnty_`cnty' == . ;
	tab demo_`cnty'high, m ;
	tab demo_`cnty'low, m ;
} ;

summ n_earnhhpre9095, det ;
replace n_earnhhpre9095 = 0 if n_earnhhpre9095 == . ;
gen byte demo_parhigh = (n_earnhhpre9095 > r(p50) & n_earnhhpre9095 != .) ;
gen byte demo_parlow = (n_earnhhpre9095 <= r(p50) & n_earnhhpre9095 != .) ;
replace demo_parhigh = . if n_earnhhpre9095 == . ;
replace demo_parlow = . if n_earnhhpre9095 == . ;
tab demo_parhigh, m ;
tab demo_parlow, m ;


/* Control means */
preserve ;
local summstats_lab "" ;
local tot = 0 ;
local demo_ct = 0 ;

foreach demo in `demo_list' { ;
	local demo_ct = `demo_ct' + 1 ;
	foreach var of varlist `vars' { ;
		local summstats_lab "`summstats_lab' `var'`demo_ct' " ;
		local tot = `tot' + 1 ;
	} ;
} ;	/* end demo */


matrix cXe_summ=J(1,`tot',.) ;
local count = 0 ;

foreach demo in `demo_list' { ;
	foreach var of varlist `vars' { ;

			local count = `count' + 1 ;
			qui summ `var' if n_post == 0 & abs(dobdd_run) <= `bwidth_close' & demo_`demo'== 1, det ;
			matrix cXe_summ[1,`count']=r(mean) ;
			
	} ;
} ; /* end demo */

clear ;
svmat cXe_summ ; 
rename (cXe_summ*) (`summstats_lab') ;
save "`output'\prwora_cXe_summstats.dta", replace ;
restore ;

/* RD variables */
gen dobdd_run2=dobdd_run*dobdd_run;
gen dobdd_run3=dobdd_run2*dobdd_run;
gen dobdd_run4=dobdd_run3*dobdd_run;

gen postXdobdd=n_post*dobdd_run;
gen postXdobdd2=n_post*dobdd_run2;
gen postXdobdd3=n_post*dobdd_run3;
gen postXdobdd4=n_post*dobdd_run4;

local poly_1 "dobdd_run postXdobdd";
local poly_2 "dobdd_run dobdd_run2 postXdobdd postXdobdd2";
local poly_3 "dobdd_run dobdd_run2 dobdd_run3 postXdobdd postXdobdd2 postXdobdd3";
local poly_4 "dobdd_run dobdd_run2 dobdd_run3 dobdd_run4 postXdobdd postXdobdd2 postXdobdd3 postXdobdd4";

foreach demo in `demo_list' { ;

foreach bwidth in `bwidth_list' {;

	forval poly = `poly_list' {;
		
		foreach cov in `cov_list' { ;
		
				
				/* RD regressions */
				
				reg n_post n_post `poly_`poly'' 
					if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1 
					, robust;
					outreg2 using "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_rd_`demo'.xls", e(N r2 rmse) replace;
				
				foreach var of varlist 
					`vars'
					{;
					xi: reg `var' n_post `poly_`poly''
						`controls_`cov''
						if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1
						, robust; 
					outreg2 using "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_rd_`demo'.xls", e(N r2 rmse) append;
					} ;	
				
		/* IV estimates */

				ivreg2 n_post (n_age18unfav=n_post) `poly_`poly''
					if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1
					, robust;
					outreg2 using  "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_iv_`demo'.xls", e(N r2 rmse) replace;
					
				/* IV using unfavorable age 18 */
				foreach var of varlist 
					`vars'
					{;
					
					qui summ `var' 
						if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1 ;
					if r(sd) != 0 { ;
						xi: ivreg2 `var' (n_age18unfav=n_post) `poly_`poly''
							`controls_`cov''
							if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1
							, robust;
						outreg2 using  "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_iv_`demo'.xls", e(N r2 rmse) append;
						} ;
					};
				
			} ; /* end cov */
				
		} ; /* end poly */
	} ; /* end bwidth*/
} ;	/* end demo */

capture log close;
